﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InfoSource.Utilities;

namespace InfoSource.Data
{
	public static class DoNotMarketListRequests
	{
		public static ResultStatus DeleteSingleDoNotMarketZipCode(int ZipCode)
		{
			ResultStatus results = new ResultStatus();

			if (ZipCode == 0)
			{
				results.ResultCode = 0;
				results.ResultMessage = "ZipCode not provided in request.";
				return results;
			}

			InfoSourceDB db = new InfoSourceDB();
			Do_Not_Market_List DNMLZip = db.Do_Not_Market_Lists.FirstOrDefault(zip => zip.Zip.Equals(ZipCode));

			if (DNMLZip == null)
			{
				results.ResultCode = 0;
				results.ResultMessage = "ZipCode: " + ZipCode + " not found in Do Not Market List.";
				return results;
			}

			try
			{
				db.Do_Not_Market_Lists.DeleteOnSubmit(DNMLZip);
				db.SubmitChanges();

				results.ResultCode = 1;
				results.ResultMessage = "Success";
			}
			catch (Exception e)
			{
				results.ResultCode = 0;
				results.ResultMessage = "Save To DataBase Failed: " + e.Message;
			}

			return results;
		}

		/*----------------------------------------------------------*/
		public static ResultStatus AddSingleDoNotMarketZipCode(int ZipCode)
		{
			ResultStatus result = new ResultStatus();

			if (ZipCode == 0)
			{
				result.ResultCode = 0;
				result.ResultMessage = "ZipCode not provided in request.";
				return result;
			}

			InfoSourceDB db = new InfoSourceDB();
			Do_Not_Market_List DNMLZip = db.Do_Not_Market_Lists.FirstOrDefault(z => z.Zip.Equals(ZipCode));

			if (DNMLZip != null)
			{
				result.ResultCode = 0;
				result.ResultMessage = "ZipCode already exists in Do Not Market List Table.";
				return result;
			}
			else
			{
				DNMLZip = new Do_Not_Market_List()
				{
					Zip = ZipCode
				};
			}

			try
			{
				db.Do_Not_Market_Lists.InsertOnSubmit(DNMLZip);
				db.SubmitChanges();

				result.ResultCode = 1;
				result.ResultMessage = "";
			}
			catch (Exception e)
			{
				result.ResultCode = 0;
				result.ResultMessage = e.Message;
			}

			return result;
		}
	}
}
